import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
import { Radio as AntdRadio } from 'antd';
import React, { useEffect } from 'react';
import createField from '../../../createField';
import useOptions from '../../../hooks/useOptions';
var _excluded = ['children', 'options', 'vertical'],
  _excluded2 = ['label'];
var Radio = function Radio(_ref) {
  var children = _ref.children,
    optionsProp = _ref.options,
    vertical = _ref.vertical,
    rest = _objectWithoutProperties(_ref, _excluded);
  var _useOptions = useOptions(optionsProp),
    options = _useOptions.options;
  useEffect(
    function () {
      var existed =
        (rest === null || rest === void 0 ? void 0 : rest.value) && options.length
          ? options.some(function (option) {
              return rest.value !== option.value;
            })
          : false;
      if ((rest === null || rest === void 0 ? void 0 : rest.value) && !existed) {
        var _rest$onChange;
        // @ts-ignore
        (_rest$onChange = rest.onChange) === null || _rest$onChange === void 0
          ? void 0
          : _rest$onChange.call(rest, undefined);
      }
    },
    [options],
  );
  return /*#__PURE__*/ React.createElement(
    AntdRadio.Group,
    _objectSpread(
      _objectSpread({}, rest),
      {},
      {
        options: options,
      },
    ),
    children
      ? children
      : options.map(function (option) {
          var label = option.label,
            rest = _objectWithoutProperties(option, _excluded2);
          return /*#__PURE__*/ React.createElement(
            AntdRadio,
            _objectSpread(
              {
                key: rest.value,
              },
              rest,
            ),
            label,
          );
        }),
  );
};
export default createField(Radio);
